package com.kx.demo.jdk.collection;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author kx
 * @date 2020/4/12
 */
public class LRUInternelLinkedHashDemo<K, V> {

    private final int maxCapacity;

    private Map<K, V> map;

    public LRUInternelLinkedHashDemo(int maxCapacity) {
        this.maxCapacity = maxCapacity;
        this.map = new LinkedHashMap<K, V>(maxCapacity, 0.75f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
                return size() > maxCapacity;
            }
        };
    }

    public V get(K key) {
        return map.get(key);
    }

    public void put(K key, V value) {
        map.put(key, value);
    }

    @Override
    public String toString() {
        return map.toString();
    }

    public static void main(String[] args) {
        LRUInternelLinkedHashDemo<Integer, Integer> lruLinkedHashMapDemo = new LRUInternelLinkedHashDemo<>(3);
        lruLinkedHashMapDemo.put(1, 111);
        lruLinkedHashMapDemo.put(2, 222);
        lruLinkedHashMapDemo.get(1);
        lruLinkedHashMapDemo.put(3, 3333);
        lruLinkedHashMapDemo.put(4, 4444);

        System.out.println(lruLinkedHashMapDemo);
    }

}
